package com.island.util;

import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;

import sun.net.www.content.text.plain;

public class DateUtils {

    private static final long DAY_LENGTH = 1000 * 60 * 60 * 24;

    /**
     * get a String represent a Date in specified format
     * @param date the date value to be formatted into a time string
     * @param pattern the format of the result
     * @return the formatted date-time string.
     * @see java.text.SimpleDateFormat
     */
    public static String getDateStr(Date date,String pattern){
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }

    /**
     * get a String represent a Date in specified format
     * @param time the date value to be formatted into a time string
     * @param pattern the format of the result
     * @return the formatted date-time string.
     * @see java.text.SimpleDateFormat
     */
    public static String getDateStr(long time,String pattern){
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(new Date(time));
    }

    /**
     * get a String represent current time in specified format
     * @param pattern the format of the result
     * @return the formatted date-time string.
     * @see java.text.SimpleDateFormat
     */
    public static String getNow(String pattern){
        return getDateStr(System.currentTimeMillis(),pattern);
    }
    /**
     * parse a date String into date, null will return when parse failed
     * @param dateStr the String to be formated
     * @param pattern the format of the date String
     * @return the parsed date
     * @see java.text.SimpleDateFormat
     */
    public static Date getDate(String dateStr,String pattern){
        SimpleDateFormat df     = new SimpleDateFormat(pattern);
        Date             output;
        try {
            output = df.parse(dateStr);
        }catch (ParseException ex) {
            output = null;
        }
        return output;
    }

    /**
     * convert a date String in one format into an other, "" will return when parse failed
     * @param dateStr the date String to be converted
     * @param patternFrom the format of the date String
     * @param patternTo the format of the String will be converted to
     * @return the converted date String
     * @see java.text.SimpleDateFormat
     */
    public static String convert(String dateStr,String patternFrom,String patternTo){
        String           output = "";
        Date             date   = getDate(dateStr,patternFrom);
        output = (null==date)?"":getDateStr(date,patternTo);
        return output;
    }

    /**
     * get "season" string for a date string. for example, "20030502" and
     *  "yyyyMMdd" were inputed, "200302" will return.
     * @param dateStr the date String
     * @param datePattern the date String format
     * @return both year and season in one string
     * @see java.text.SimpleDateFormat
     */
    public static String getSeason(String dateStr,String datePattern) {
        String            output  = "";
        Date              date    = getDate(dateStr,datePattern);
        GregorianCalendar cal     = new GregorianCalendar();
        String            pattern = "yyyy";
        SimpleDateFormat  df      = new SimpleDateFormat(pattern);
        output += df.format(date);
        cal.setTime(date);
        output += getSeason(cal.get(GregorianCalendar.MONTH));
        return output;
    }

    /**
     * get the season String
     * @param month the month of year
     * @return only season string
     * @see java.util.GregorianCalendar
     */
    private static String getSeason(int month){
        switch (month) {
            case GregorianCalendar.JANUARY:
                return "01";
            case GregorianCalendar.FEBRUARY:
                return "02";
            case GregorianCalendar.MARCH:
                return "03";
            case GregorianCalendar.APRIL:
                return "04";
            case GregorianCalendar.MAY:
                return "05";
            case GregorianCalendar.JUNE:
                return "06";
            case GregorianCalendar.JULY:
                return "07";
            case GregorianCalendar.AUGUST:
                return "08";
            case GregorianCalendar.SEPTEMBER:
                return "09";
            case GregorianCalendar.OCTOBER:
                return "10";
            case GregorianCalendar.NOVEMBER:
                return "11";
            case GregorianCalendar.DECEMBER:
                return "12";
        }
        return "";
    }

    public static int getDayInterval(long end, long start){
        int day = (int)((end-start)/(DAY_LENGTH));
        return day;
    }

    public static long addDays(long time, int day){
        return time + day * DAY_LENGTH;
    }

    public static long startOfDay(long time){
        String day = getDateStr(time,"yyyyMMdd");
        return getDate(day,"yyyyMMdd").getTime();
    }

    public static long endOfDay(long time){
        return startOfDay(time) + DAY_LENGTH - 1;
    }
    
    public static void main(String[] args) {
		System.out.println(getSeason("20120202",""));
	}

}